//
//  TLCustomNavBarViewController.m
//  FunnyVideo
//
//  Created by 李鑫 on 15/10/13.
//  Copyright © 2015年 NewEggTech. All rights reserved.
//

#import "TLCustomNavBarViewController.h"

@interface NSString (NavTitle)
+ (TLCustomNavTitleType)navTitleStrToType:(NSString *)title;
+ (UIImage *)navTitleImageType:(TLCustomNavTitleType)type;
@end


/**
 #define Nav_Img_Logo @"NavImgLogo"
 #define Nav_Img_All  @"NavImgAll"
 #define Nav_Img_Video @"NavImgVideo"
 #define Nav_Img_Comment @"NavImgCmt"
 #define Nav_Img_Feed    @"NavImgFeed"
 #define Nav_Img_Record  @"NavImgRcd"
 #define Nav_Img_Msg     @"NavImgMsg"
 #define Nav_Img_FgPsw   @"NavImgFgPsw"
 
 TLCustomNavTitleTypeLogo = 0,
 TLCustomNavTitleTypeAll,
 TLCustomNavTitleTypeVideo,
 TLCustomNavTitleTypeComment,
 TLCustomNavTitleTypeFeed,
 TLCustomNavTitleTypRecord,
 TLCustomNavTitleTypeMsg,
 TLCustomNavTitleTypeFgPsw,
 */

@implementation NSString (NavTitle)
+ (TLCustomNavTitleType)navTitleStrToType:(NSString *)title{
    if([title isEqualToString:Nav_Img_Logo])
        return TLCustomNavTitleTypeLogo;
    else if([title isEqualToString:Nav_Img_All])
        return TLCustomNavTitleTypeAll;
    else if([title isEqualToString:Nav_Img_Video])
        return TLCustomNavTitleTypeVideo;
    else if([title isEqualToString:Nav_Img_Comment])
        return TLCustomNavTitleTypeComment;
    else if([title isEqualToString:Nav_Img_Feed])
        return TLCustomNavTitleTypeFeed;
    else if([title isEqualToString:Nav_Img_Record])
        return TLCustomNavTitleTypRecord;
    else if([title isEqualToString:Nav_Img_Msg])
        return TLCustomNavTitleTypeMsg;
    else
        return TLCustomNavTitleTypeFgPsw;
}
+ (UIImage *)navTitleImageType:(TLCustomNavTitleType)type{
    switch (type) {
        case TLCustomNavTitleTypeLogo:
            return [UIImage imageNamed:Nav_Img_Logo];
            break;
        case TLCustomNavTitleTypeAll:
            return [UIImage imageNamed:Nav_Img_All];
            break;
        case TLCustomNavTitleTypeVideo:
            return [UIImage imageNamed:Nav_Img_Video];
            break;
        case TLCustomNavTitleTypeComment:
            return [UIImage imageNamed:Nav_Img_Comment];
            break;
        case TLCustomNavTitleTypeFeed:
            return [UIImage imageNamed:Nav_Img_Feed];
            break;
        case TLCustomNavTitleTypRecord:
            return [UIImage imageNamed:Nav_Img_Record];
            break;
        case TLCustomNavTitleTypeMsg:
            return [UIImage imageNamed:Nav_Img_Msg];
            break;
        case TLCustomNavTitleTypeFgPsw:
            return [UIImage imageNamed:Nav_Img_All];
            break;
        default:
            return [UIImage imageNamed:Nav_Img_Logo];
            break;
    }
    
}
@end

@interface TLCustomNavBarViewController ()

@end

@implementation TLCustomNavBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:240/250.f green:240/250.f blue:240/250.f alpha:1];
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationItem.titleView = [self logoTitleView];
}


- (UIView *)customTitleView:(TLCustomNavTitleType)type{
    UIImage *header = [NSString navTitleImageType:type];
    
    NSLog(@"%@",NSStringFromCGSize([TLDeviceManger navigationBarSize]));
    CGFloat logoWidth = header.size.width*0.8;
    CGFloat logoHeight = header.size.height*0.8;
    
    UIView *logo = [[UIView alloc] initWithFrame:CGRectMake(([TLDeviceManger navigationBarSize].width - logoWidth)/2, ([TLDeviceManger navigationBarSize].height - logoHeight)/2, logoWidth,logoHeight)];
    logo.backgroundColor = [UIColor clearColor];
    logo.layer.contents = (id)header.CGImage;
    
    return logo;
}

- (UIView *)logoTitleView{
    return [self customTitleView:TLCustomNavTitleTypeLogo];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -- Set Navigation Title View Type
- (void)setTitle:(NSString * __nullable)title{
    TLCustomNavTitleType navTitleViewType = [NSString navTitleStrToType:title];
    
    self.navigationItem.titleView = [self customTitleView:navTitleViewType];
}

- (void)setNavTitleType:(TLCustomNavTitleType)type{
    self.navigationItem.titleView = [self customTitleView:type];
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
